package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;

/**
 * Created by Administrator on 2016/6/22.
 */
public class ConfirmShortNumberDialog extends Dialog {
    TextView content_tv;
    private int phoneNum = 3;    //1-号码1是短号、2-号码2是短号、3-两个号码都是短号
    ConfirmShortNumberDialogCallback callback;

    public ConfirmShortNumberDialog(Context context, ConfirmShortNumberDialogCallback callback) {
        super(context);
        this.callback = callback;
    }

    public ConfirmShortNumberDialog(Context context, int phoneNum, ConfirmShortNumberDialogCallback callback) {
        super(context);
        this.callback = callback;
        this.phoneNum = phoneNum;
    }

    public ConfirmShortNumberDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ConfirmShortNumberDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_confirm_short_dialog);

        setDialogAttributes();
        initView();
    }

    private void initView() {
        String contentStr;
        content_tv = (TextView) findViewById(R.id.content_tv);
        if (phoneNum == 1) {
            contentStr = getContext().getResources().getString(R.string.short_number_confirm_notice_1) + "1" +
                    getContext().getResources().getString(R.string.short_number_confirm_notice_2);
        } else if (phoneNum == 2) {
            contentStr = getContext().getResources().getString(R.string.short_number_confirm_notice_1) + "2" +
                    getContext().getResources().getString(R.string.short_number_confirm_notice_2);
        } else {
            contentStr = getContext().getResources().getString(R.string.short_number_confirm_notice_1) +
                    getContext().getResources().getString(R.string.short_number_confirm_notice_2);
        }
        content_tv.setText(contentStr);

        findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.save();
                dismiss();
            }
        });
        findViewById(R.id.cancle_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.cancle(phoneNum);
                dismiss();
            }
        });
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setCancelable(false);
    }
}
